//
//  TagsSecondView.m
//  UEAssistant
//
//  Created by mac on 2017/7/17.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "TagsSecondView.h"
#import "YZTagList.h"

@interface TagsSecondView ()

@property (nonatomic, strong) UIButton          *resetBt;
@property (nonatomic, strong) UIButton          *sureBt;
@property (nonatomic, strong) UIView            *line;
@property (nonatomic, strong) YZTagList         *tagList;
@property (nonatomic, strong) NSMutableArray    *tags;
@property (nonatomic, strong) NSMutableArray    *tagNames;

@end

@implementation TagsSecondView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setUI{

     self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    _tags = [NSMutableArray array];
    _tagNames = [NSMutableArray array];
    [AvailableNetWorking postWithUrl:transLabels_connector params:@{} successHandler:^(id responseObject) {
        
        NSLog(@"tags == %@",responseObject);
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray * arr = responseObject[@"result"];
            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [_tagList addTag:obj[@"labelName"]];
            }];
        }
    } falureHandler:^(NSError *error) {
        
    }];

    
    [self addSubview:self.resetBt];
    [self addSubview:self.sureBt];
    [self addSubview:self.line];
    
    YZTagList *tagList = [[YZTagList alloc] init];
    tagList.backgroundColor = [UIColor whiteColor];
    _tagList = tagList;
    _tagList.frame = CGRectMake(0, 10, self.bounds.size.width, 0);
    _tagList.tagBackgroundColor = [UIColor whiteColor];
    _tagList.tagColor = RGB(142, 157, 171, 1);
    _tagList.tagFont = [UIFont systemFontOfSize:FLEXIBLE(15)];
    [self addSubview:_tagList];
    
    __weak typeof(_tags) weakTags = _tags;
    __weak typeof(_tags) weakTagsName = _tagNames;
    __weak typeof(self) weakSelf = self;
    
    _tagList.clickTagBlock = ^(NSString *tag){
        NSLog(@"选中的标签是：%@",tag);
        if (![weakTagsName containsObject:tag]) {
            [weakTagsName addObject:tag];
            [weakTags addObject:tag];
        }
        if (self.didSelectSectionHandler != nil) {
            weakSelf.didSelectSectionHandler(tag);
        }
    };
}

#pragma mark -- Event

- (void)resetTags{
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"resetTags" object:nil];
    //恢复默认(注意要清空上次选中的标签数据)
    [_tags removeAllObjects];
    [_tagList resetUserface];
    if (self.didSelectSectionHandler != nil) {
        self.didSelectSectionHandler(@"重置");
    }
}

- (void)sureTags{
    //POST
    [[NSNotificationCenter defaultCenter]postNotificationName:@"dismissTagsView" object:nil];
}

- (void)setDidSelectDayHandler:(DidSelectSectionHandler)didSelectSectionHandler {
    
    _didSelectSectionHandler = didSelectSectionHandler;
    self.didSelectDayHandler = _didSelectSectionHandler; // 传递 block
    
}

#pragma mark -- getter

- (UIButton *)resetBt{
    
    if (!_resetBt) {
        _resetBt = [UIButton buttonWithType:UIButtonTypeSystem];
        _resetBt.frame = CGRectMake(15, Screen_Height-64-44-70, 80, 40);
        _resetBt.backgroundColor = RGB(237,239, 240, 1);
        [_resetBt setTitleColor:RGB(0, 0, 33, 1) forState:UIControlStateNormal];
        [_resetBt setTitle:@"重置" forState:UIControlStateNormal];
        _resetBt.layer.cornerRadius = 5;
        [_resetBt addTarget:self action:@selector(resetTags) forControlEvents:UIControlEventTouchUpInside];
    }
    return _resetBt;
}

- (UIButton *)sureBt{
    
    if (!_sureBt) {
        _sureBt = [UIButton buttonWithType:UIButtonTypeSystem];
        _sureBt.frame = CGRectMake(_resetBt.frame.origin.x+_resetBt.frame.size.width+10, Screen_Height-64-44-70, Screen_Width-15-_resetBt.frame.size.width-10-15, 40);
        _sureBt.backgroundColor = RGB(57,186, 241, 1);
        [_sureBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureBt setTitle:@"确认" forState:UIControlStateNormal];
        _sureBt.layer.cornerRadius = 5;
        [_sureBt addTarget:self action:@selector(sureTags) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _sureBt;
}

- (UIView *)line{
    
    if (!_line) {
        _line = [[UIView alloc]initWithFrame:CGRectMake(0, Screen_Height-64-44-80, Screen_Width, 1)];
        _line.backgroundColor = RGB(236, 236, 236, 1);
    }
    return _line;
}


@end
